// Copyright 2008 Senary
// See credits.txt for details.

// This file is part of Omega VII.

// Omega VII is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Omega VII is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Omega VII.  If not, see <http://www.gnu.org/licenses/>.

#include "star.h"
#include "MediaManager.h"
#include "SpaceObject.h"

Star::Star()
{
}

Star::~Star()
{
}

void Star::enhanceParticle(PARTICLE* p) {
	p->bmap=MediaManager::getInstance()->getImage("star.bmp");
	p->SUN_CENTER_X = p->x;
	p->SUN_CENTER_Y = p->y;
	p->SUN_CENTER_Z = p->z;
	p->flags|=TRANSLUCENT;
	p->alpha=_VAR(10);
	p->size=SUN_PARTICLE_SIZE;
}

void Star::moveParticle(PARTICLE* p) {
	p->x=fsin(p->A, fcos(p->B,p->R)) + p->SUN_CENTER_X;
	p->y=fcos(p->A,fcos(p->B,p->R)) + p->SUN_CENTER_Y;
	p->z=fsin(p->B,p->R)+p->SUN_CENTER_Z;
}

void Star::sun2(PARTICLE* p) {
	p->A += p->RANDOM_PARTICLE_DRIFT;
	p->B += p->RANDOM_PARTICLE_DRIFT;
	moveParticle(p);
	p->lifespan=_VAR(10);
}	

void Star::sun(PARTICLE* p) {
	enhanceParticle(p);
	p->A=random(_VAR(360));
	p->B=random(_VAR(360));
	p->R=MAX_SUN_RADIUS;
	p->RANDOM_PARTICLE_DRIFT = random(DRIFT_FACTOR) + _VAR(0.1);
	moveParticle(p);
	p->event=(EVENT)sun2;
}

void Star::sunCenter(PARTICLE* p) {
	enhanceParticle(p);
	p->A=random(_VAR(360));
	p->B=random(_VAR(360));
	p->R=random(MAX_SUN_RADIUS);
	p->RANDOM_PARTICLE_DRIFT=random(DRIFT_FACTOR)+_VAR(0.1);
	moveParticle(p);
	p->event=(EVENT)sun2;
}


void Star::createStar(SpaceObject* so) {
	ENTITY* light;
	
	randomize();

	//Create the Sun
	VECTOR pos;
	so->getPosition().toGamestudio(&pos);

	effect((EVENT)sunCenter, N_SUN_CENTER_PARTICLES, &pos, NULL);
	effect((EVENT)sun, N_SUN_PARTICLES, &pos, NULL);

	light = ent_create("star.bmp",&pos,NULL);
	light->flags|=FACING|BRIGHT|TRANSLUCENT;
	light->alpha=_VAR(20);

	light->red = _VAR(170);
	light->green = _VAR(100);
	light->blue = _VAR(150);

	light->lightrange = _VAR(700);
}